package y_2025.m03.d01;

public class PageDigitCounter {
    public static void countDigits(int n) {
        int[] counts = new int[10];
        int length = (int)Math.log10(n) + 1;
        
        // 统计1到999...999（n位全9）的数字出现次数
        for (int i = 1; i <= length; i++) {
            int power = (int)Math.pow(10, i - 1);
            int high = n / (int)Math.pow(10, i);
            int low = n % (int)Math.pow(10, i - 1);
            
            for (int d = 0; d < 10; d++) {
                counts[d] += high * power;
            }
            for (int d = 0; d < high; d++) {
                counts[d] += (int)Math.pow(10, i - 1);
            }
            counts[high] += low + 1;
            
            // 减去前导零（仅当i < length时）
            if (i < length) {
                counts[0] -= (int)Math.pow(10, i - 1);
            }
        }
        
        // 处理余数部分（如n=34789时，余数为4789）
        int remainder = n % (int)Math.pow(10, length - 1);
        if (remainder > 0) {
            for (int d = 0; d < remainder; d++) {
                counts[d] += (int)Math.pow(10, length - 1);
            }
        }
        
        // 输出结果
        for (int i = 0; i < 10; i++) {
            System.out.println("数字" + i + "出现次数：" + counts[i]);
        }
    }
    
    public static void main(String[] args) {
        countDigits(34789); // 示例输入
    }
}